package com.tyut.controller;


import com.tyut.common.constant.JwtClaimsConstant;
import com.tyut.common.properties.JwtProperties;
import com.tyut.common.result.PageResult;
import com.tyut.common.result.Result;
import com.tyut.common.utils.JwtUtil;
import com.tyut.domain.User;
import com.tyut.dto.UserDTO;
import com.tyut.dto.UserLoginDTO;
import com.tyut.dto.UserPageQueryDTO;
import com.tyut.service.UserService;
import com.tyut.vo.UserLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/Singer/")
public class SingerController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;


    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("登录：{}", userLoginDTO);

        User user = userService.login(userLoginDTO);


        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        UserLoginVO employeeLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .token(token)
                .build();


        return Result.success(employeeLoginVO);
    }

    /**
     * 用户退出
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "退出")
    public Result<String> logout() {
        return Result.success();
    }


    /**
     * 新增歌手用户
     * @param userDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增歌手用户")
    public Result addSinger(@RequestBody UserDTO userDTO) {
        userService.add(userDTO);
        return Result.success();
    }
    /**
     * 实现歌手的条件分页查找
     *
     * @param
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("歌手分页查询")
    public Result<PageResult> page2(UserPageQueryDTO userPageQueryDTO) {
        //记录日志
        log.info("员工条件分页查询，参数：{}", userPageQueryDTO);
        //调用业务层分层查询功能

        PageResult pageResult = userService.pageQuery2(userPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 启用禁用歌手账号
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用歌手账号")
    public Result<String> startOrStop(@PathVariable("status") Integer status, int id) {
        //或者这样写亦可：public Result startOrStop(@PathVariable Integer status,long id){
        //但要求{status}内的参数即传入的参数和小括号内的形参名保持一致，否则就只能用上面的形式。
        userService.startOrStop(status, id);
        return Result.success();
    }
    /**
     * 修改员工信息 1.根据id查询员工信息(用于修改员工信息时的回显。)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户信息")
    public Result<User> getById(@PathVariable Integer id){
        User user = userService.getById(id);
        return Result.success(user);
    }
    /**
     * 修改用户信息 2.根据回显内容重新编辑员工信息
     * @param userDTO
     * @return
     */
    @PutMapping
    @ApiOperation("重新编辑用户信息")
    public Result<User> updateUser(@RequestBody UserDTO userDTO){
        log.info("编辑用户信息:{}",userDTO);
        userService.updateUser(userDTO);
        return Result.success();
    }

}
